#ifndef __KEYEVENTS_H  
#define __KEYEVENTS_H  

	protected void keyPressed(int keyCode)
	{
		switch(keyCode)
		{
			case KEYS.KEY_LSK:
				keyflags |= KEYS.F_KEY_LSK;
			break;
			case KEYS.KEY_RSK:
				keyflags |= KEYS.F_KEY_RSK;
			break;
			case KEYS.KEY_FIRE:
				keyflags |= KEYS.F_KEY_FIRE;
			break;
			case KEYS.KEY_LEFT:
				keyflags |= KEYS.F_KEY_LEFT;
			break;
			case KEYS.KEY_RIGHT:
				keyflags |= KEYS.F_KEY_RIGHT;
			break;
			case KEYS.KEY_UP:
				keyflags |= KEYS.F_KEY_UP;
			break;
			case KEYS.KEY_DOWN:
				keyflags |= KEYS.F_KEY_DOWN;
			break;
			case KEYS.KEY_0:
				keyflags |= KEYS.F_KEY_0;
			break;
			case KEYS.KEY_1:
				keyflags |= KEYS.F_KEY_1;
			break;
			case KEYS.KEY_2:
				keyflags |= KEYS.F_KEY_2;
			break;
			case KEYS.KEY_3:
				keyflags |= KEYS.F_KEY_3;
			break;
			case KEYS.KEY_4:
				keyflags |= KEYS.F_KEY_4;
			break;
			case KEYS.KEY_5:
				keyflags |= KEYS.F_KEY_5;
			break;			
			case KEYS.KEY_6:
				keyflags |= KEYS.F_KEY_6;
			break;
			case KEYS.KEY_7:
				keyflags |= KEYS.F_KEY_7;
			break;
			case KEYS.KEY_8:
				keyflags |= KEYS.F_KEY_8;
			break;
			case KEYS.KEY_9:
				keyflags |= KEYS.F_KEY_9;
			break;
			case KEYS.KEY_POUND:
				keyflags |= KEYS.F_KEY_POUND;
			break;
			case KEYS.KEY_STAR:
				keyflags |= KEYS.F_KEY_STAR;
			break;
		}
	}
	
	protected void keyReleased(int keyCode)
	{
		switch(keyCode)
		{
			case KEYS.KEY_LSK:
				keyflags &=~ KEYS.F_KEY_LSK;
			break;
			case KEYS.KEY_RSK:
				keyflags &=~ KEYS.F_KEY_RSK;
			break;
			case KEYS.KEY_FIRE:
				keyflags &=~ KEYS.F_KEY_FIRE;
			break;
			case KEYS.KEY_LEFT:
				keyflags &=~ KEYS.F_KEY_LEFT;
			break;
			case KEYS.KEY_RIGHT:
				keyflags &=~ KEYS.F_KEY_RIGHT;
			break;			
			case KEYS.KEY_UP:
				keyflags &=~ KEYS.F_KEY_UP;
			break;
			case KEYS.KEY_DOWN:
				keyflags &=~ KEYS.F_KEY_DOWN;
			break;			
			case KEYS.KEY_0:
				keyflags &=~ KEYS.F_KEY_0;
			break;
			case KEYS.KEY_1:
				keyflags &=~ KEYS.F_KEY_1;
			break;
			case KEYS.KEY_2:
				keyflags &=~ KEYS.F_KEY_2;
			break;
			case KEYS.KEY_3:
				keyflags &=~ KEYS.F_KEY_3;
			break;
			case KEYS.KEY_4:
				keyflags &=~ KEYS.F_KEY_4;
			break;
			case KEYS.KEY_5:
				keyflags &=~ KEYS.F_KEY_5;
			break;			
			case KEYS.KEY_6:
				keyflags &=~ KEYS.F_KEY_6;
			break;
			case KEYS.KEY_7:
				keyflags &=~ KEYS.F_KEY_7;
			break;
			case KEYS.KEY_8:
				keyflags &=~ KEYS.F_KEY_8;
			break;
			case KEYS.KEY_9:
				keyflags &=~ KEYS.F_KEY_9;
			break;
			case KEYS.KEY_POUND:
				keyflags &=~ KEYS.F_KEY_POUND;
			break;
			case KEYS.KEY_STAR:
				keyflags &=~ KEYS.F_KEY_STAR;
			break;
		}
	}  
	
	public static boolean isKeyPressed(int keyCode)
	{
		switch(keyCode)
		{
			case KEYS.KEY_LSK:
				if((keyflags&KEYS.F_KEY_LSK)!=0)
					return true;
			break;
			case KEYS.KEY_RSK:
				if((keyflags&KEYS.F_KEY_RSK)!=0)
					return true;
			break;
			case KEYS.KEY_FIRE:
				if((keyflags&KEYS.F_KEY_FIRE)!=0)
					return true;
			break;
			case KEYS.KEY_LEFT:
				if((keyflags&KEYS.F_KEY_LEFT)!=0)
					return true;
			break;
			case KEYS.KEY_RIGHT:
				if((keyflags&KEYS.F_KEY_RIGHT)!=0)
					return true;
			break;			
			case KEYS.KEY_UP:
				if((keyflags&KEYS.F_KEY_UP)!=0)
					return true;
			break;
			case KEYS.KEY_DOWN:
				if((keyflags&KEYS.F_KEY_DOWN)!=0)
					return true;
			break;			
			case KEYS.KEY_0:
				if((keyflags&KEYS.F_KEY_0)!=0)
					return true;
			break;
			case KEYS.KEY_1:
				if((keyflags&KEYS.F_KEY_1)!=0)
					return true;
			break;
			case KEYS.KEY_2:
				if((keyflags&KEYS.F_KEY_2)!=0)
					return true;
			break;
			case KEYS.KEY_3:
				if((keyflags&KEYS.F_KEY_3)!=0)
					return true;
			break;
			case KEYS.KEY_4:
				if((keyflags&KEYS.F_KEY_4)!=0)
					return true;
			break;
			case KEYS.KEY_5:
				if((keyflags&KEYS.F_KEY_5)!=0)
					return true;
			break;			
			case KEYS.KEY_6:
				if((keyflags&KEYS.F_KEY_6)!=0)
					return true;
			break;
			case KEYS.KEY_7:
				if((keyflags&KEYS.F_KEY_7)!=0)
					return true;
			break;
			case KEYS.KEY_8:
				if((keyflags&KEYS.F_KEY_8)!=0)
					return true;
			break;
			case KEYS.KEY_9:
				if((keyflags&KEYS.F_KEY_9)!=0)
					return true;
			break;
			case KEYS.KEY_POUND:
				if((keyflags&KEYS.F_KEY_POUND)!=0)
					return true;
			break;
			case KEYS.KEY_STAR:
				if((keyflags&KEYS.F_KEY_STAR)!=0)
					return true;
			break;
		}
		
		return false;
	}
	
	final static char[][] CHARS = {
						{' '},
						{'.',',','!','?','1'},
						{'a','b','c','2'},
						{'d','e','f','3'},
						{'g','h','i','4'},
						{'j','k','l','5'},
						{'m','n','o','6'},
						{'p','q','r','s','7'},
						{'t','u','v','8'},
						{'w','x','y','z','9'}
						};
						
	public static long lastInput = 0;
	public static int lastPress = 48;
	public static int pressOff = 0;
	
	public String input(String str, int maxChars)
	{
		if(System.currentTimeMillis() - lastInput > KEYS.INPUT_THRESHOLD && str.length() < maxChars)
		{
			for(int i = KEYS.KEY_0; i <= KEYS.KEY_9; i++)
			{
				if(isKeyPressed(i))
				{
					if(i == lastPress && System.currentTimeMillis() - lastInput < 2000 && str.length() > 0)
					{
						str = str.substring(0, str.length() - 1);
						pressOff++;
					}
					else
					{
						pressOff = 0;
					}
					
					if(pressOff >= CHARS[i - KEYS.KEY_0].length)
						pressOff -= CHARS[i - KEYS.KEY_0].length;
					
					str = str + CHARS[i - KEYS.KEY_0][pressOff];
					lastPress = i;
					lastInput = System.currentTimeMillis();
				}
			}
		}
		
		if(System.currentTimeMillis() - lastInput > KEYS.INPUT_THRESHOLD)
		{
			if(isKeyPressed(KEYS.KEY_LEFT) && str.length() > 0)
			{
				lastInput = System.currentTimeMillis();
				pressOff = 0;
				str = str.substring(0, str.length() - 1);
			}
		}
		
		return str;
	}
	
	public void clearKeys()
	{
		keyflags = 0;
	}
  
#endif // __KEYEVENTS_H  
